package com.qqtop10.manager.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qqtop10.manager.dao.AutoIncMapper;
import com.qqtop10.manager.dao.ShopMapper;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Shop;
import com.qqtop10.manager.model.temp.ShopTemp;
import com.qqtop10.manager.service.ShopService;
import com.qqtop10.manager.utils.http.HttpHelper;
import com.qqtop10.manager.utils.http.entity.CustomerService;
import com.qqtop10.manager.utils.http.entity.OauthBody.OauthInfo;
import com.qqtop10.manager.utils.http.entity.SellerClassify;

@Service("shopService")
public class ShopServiceImpl extends BaseService implements ShopService {

	@Autowired
	private ShopMapper shopMapper;

	@Autowired
	private AutoIncMapper autoMapper;

	@Override
	public List<Shop> all(Shop input, PagingArgs pagingArgs) {
		return shopMapper.all(asMap(input, pagingArgs));
	}

	@Override
	public int count(Shop input) {
		return shopMapper.count(input);
	}

	@Override
	public Shop get(Shop input) {
		return shopMapper.get(input);
	}

	public int count2(Shop input) {
		return this.shopMapper.count2(input);
	}

	@Override
	public boolean approve(Shop input) {
		return shopMapper.approve(input) != 0;
	}

	@Override
	public boolean update(Shop input) {
		return shopMapper.update(input) != 0;
	}

	@Override
	public int insert(Shop record) {
		return shopMapper.insert(record);
	}

	@Override
	public int insertSelective(Shop record) {
		return shopMapper.insert(record);
	}

	@Override
	public int updateBySelective(Shop record) {
		return shopMapper.updateBySelective(record);
	}

	@Override
	public List<Shop> selectSimple() {
		return shopMapper.selectSimple();
	}

	@Override
	public Shop selectByUuid(String uuid) {
		return shopMapper.selectByUuid(uuid);
	}

	@Override
	public Shop selectByUserUuid(String uuid) {
		return shopMapper.selectByUserUuid(uuid);
	}

	@Override
	public int updateByUserUuidSelective(Shop shop) {
		return shopMapper.updateByUserUuidSelective(shop);
	}

	@Override
	public int applyXNShopId(Shop shop) {
		if (shop.getXn_account() == null || "".equals(shop.getXn_account())) {
			OauthInfo oauthInfo = HttpHelper.getOauthInfo();
			// 获取token
			String accessToken = oauthInfo.getAccess_token();
			ShopTemp shopTemp = shopMapper.selectAccount(shop.getId());
			shopTemp.setAccount(getLastTp());
			List<SellerClassify> list = HttpHelper.getSellerClassifyList(accessToken);
			SellerClassify classify = null;
			if (list != null && list.size() > 0) {
				classify = list.get(0);
				int id = classify.getId();
				CustomerService customer = HttpHelper.getCustomerServiceInfo(accessToken, id, shopTemp);
				Map<String, String> map = new HashMap<>();
				map.put("id", shop.getId().toString());
				map.put("settingid", customer.getSetting_id());
				map.put("sellerid", customer.getSite_id());
				map.put("xn_account", shopTemp.getAccount());
				return shopMapper.setCustomSerivceInfo(map);
			}
		} else {
			Map<String, String> map = new HashMap<>();
			map.put("id", shop.getId().toString());
			map.put("settingid", shop.getSettingid());
			map.put("sellerid", shop.getSellerid());
			map.put("xn_account", shop.getXn_account());
			return shopMapper.setCustomSerivceInfo(map);
		}
		return 0;
	}

	private String getLastTp() {
		autoMapper.save();
		return "TP" + autoMapper.getInc();
	}

}
